home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 001a / seyon213.tz / seyon213 / seyon / Seyon.c < prev    next >
C/C++ Source or Header  |  1993-04-10  |  8KB  |  356 lines

  1.  
  2. /*
  3.  * This file is part of the Seyon, Copyright (c) 1992-1993 by Muhammad M.
  4.  * Saggaf. All rights reserved.
  5.  *
  6.  * See the file COPYING (1-COPYING) or the manual page seyon(1) for a full
  7.  * statement of rights and permissions for this program.
  8.  */
  9.  
  10. #include <signal.h>
  11.  
  12. #include <X11/Intrinsic.h>
  13. #include <X11/StringDefs.h>
  14.  
  15. #include "seyon.h"
  16. #include "SeDecl.h"
  17.  
  18. #if HAVE_TERMIOS
  19. #include <termios.h>
  20. struct termios  newmode,
  21.                 oldmode;
  22. #else
  23. #if HAVE_TERMIO
  24. #include <termio.h>
  25. struct termio   newmode,
  26.                 oldmode;
  27. #else
  28. #if HAVE_SGTTYB
  29. #include <sys/ioctl.h>
  30. struct sgttyb   newmode,
  31.                 oldmode;
  32. #endif
  33. #endif
  34. #endif
  35.  
  36. #define SEYON_EMU_NAME    "seyon-emu"
  37.  
  38. #include "version.h"
  39. static char     version[] = "$Revision: 2.13 $";
  40.  
  41. extern Widget   CreateCommandCenter();
  42. extern void     SetIcon(),
  43.                 GetResources(),
  44.                 InitVariables();
  45. extern void     PopupInitError(),
  46.                 PopupFatalError();
  47.  
  48. void            s_script(),
  49.                 s_set(),
  50.                 s_exit(),
  51.                 die(),
  52.                 cleanup_exit();
  53.  
  54. int             tfd,
  55.                 child_pipe[2],
  56.                 scriptToMainPipe[2],
  57.                 mainToTermPipe[2];
  58. FILE           *tfp;
  59.  
  60. struct QueryResources qres;
  61. XtAppContext    app_con;
  62. Widget          topLevel;
  63. pid_t           mainPid;
  64.  
  65. int
  66. main(argc, argv)
  67.      int             argc;
  68.      char           *argv[];
  69. {
  70.   int             OpenModem();
  71.   void            DispatchActions(),
  72.                   GetParameters(),
  73.                   ShowOpenModemErrMsg(),
  74.                   exit();
  75.  
  76.   char           *arg[REG_BUF], termEmu[REG_BUF];
  77.   int             sepIndex, i, n, retStatus;
  78. #ifdef HAVE_FAS
  79.   char            large_string[1024];
  80. #endif
  81.  
  82.   char            modemList[LRG_BUF], 
  83.                  *curModem, 
  84.                  *startupAction = modemList;
  85.  
  86.   static char    *fallbackResources[] = {
  87. #include "Seyon.ad.h"
  88.     NULL,
  89.   };
  90.  
  91.   static XrmOptionDescRec optionList[] =  {
  92.     {"-modems", "modems", XrmoptionSepArg, NULL},
  93.     {"-script", "script", XrmoptionSepArg, NULL},
  94.     {"-entries", "defaultPhoneEntries", XrmoptionSepArg, NULL},
  95.     {"-dial", "dialAutoStart", XrmoptionNoArg, "True"},
  96.     {"-nodial", "dialAutoStart", XrmoptionNoArg, "False"},
  97.     {"-emulator", "emulator", XrmoptionSepArg, ""},
  98.     {"-noemulator", "noemulator", XrmoptionNoArg, ""},
  99.     {"-nodefargs", "nodefargs", XrmoptionNoArg, ""},
  100.   };
  101.   
  102.   for (i = 1; i < argc && strcmp(argv[i], "--"); i++);
  103.   sepIndex = i;
  104.  
  105.   /* Find if the -noemulator switch is given */
  106.   for (i = 1; i < sepIndex && 
  107.        strncmp(argv[i], "-noemulator", max(4, strlen(argv[i]))); i++);
  108.  
  109.   /* If no, launch Seyon via the emulator */
  110.   if (i >= sepIndex) {
  111.  
  112.     for (i = 1; i < sepIndex &&
  113.          strncmp(argv[i], "-emulator", max(3, strlen(argv[i]))); i++);
  114.     if (i < sepIndex - 1)
  115.       strcpy(termEmu, argv[i+1]);
  116.     else
  117.       strcpy(termEmu, SEYON_EMU_NAME);
  118.  
  119.     /* Find if the -nodefargs switch is given */
  120.     for (i = 1; i < sepIndex && 
  121.          strncmp(argv[i], "-nodefargs", max(5, strlen(argv[i]))); i++);
  122.  
  123.     n = 1;
  124.  
  125.     /* If no, use the default emulator arguments */
  126.     if (i >= sepIndex) {
  127.       arg[n] = "-name"; n++;
  128.       arg[n] = "Seyon"; n++;
  129.       arg[n] = "-T"; n++;
  130.       arg[n] = "Seyon Terminal Emulator"; n++;
  131.       arg[n] = "-n"; n++;
  132.       arg[n] = "Terminal"; n++;
  133.     }
  134.  
  135.     /* Pass all switches aftetr '--' to the emulator */
  136.     for (i = sepIndex + 1; i < argc; i++, n++)
  137.       arg[n] = argv[i];
  138.     
  139.     arg[n] = "-e"; n++;
  140.  
  141. #ifndef HAVE_FAS
  142.     arg[n] = argv[0]; n++;
  143.     arg[n] = "-noemulator"; n++;
  144.  
  145.     /* Pass all switches before '--' to Seyon */
  146.     for (i = 1; i < sepIndex; i++, n++)
  147.       arg[n] = argv[i];
  148. #else
  149.     arg[n] = "/bin/sh";    n++;
  150.     arg[n] = "-c"; n++;
  151.     arg[n] = large_string;
  152.     strcpy(arg[n], argv[0]);
  153.     strcat(arg[n], " -noemulator");
  154.     for (i = 1; i < sepIndex; i++) {
  155.       strcat(arg[n]," ");
  156.       strcat(arg[n],argv[i]);
  157.     }
  158.     n++;
  159. #endif
  160.  
  161.     arg[n] = NULL; n++;
  162.  
  163.     arg[0] = termEmu;
  164.     execvp(arg[0], arg);
  165.  
  166.     fprintf(stderr, "%s `%s.\n%s\n", ">> Warning: Could not execute", 
  167.             termEmu, ">> Notice: Falling to `xterm'.");
  168.  
  169.     arg[0] = "xterm";
  170.     execvp(arg[0], arg);
  171.     
  172.     fprintf(stderr, "%s\n%s\n", ">> Error: Could not execute `xterm'.",
  173.             ">> Notice: Giving up.");
  174.     exit(1);
  175.   }  /* if (i >= sepIndex)... */
  176.  
  177.   /* The real program begins here */
  178.  
  179.   topLevel = XtAppInitialize(&app_con, "Seyon", optionList,
  180.                              XtNumber(optionList), &argc, argv,
  181.                              fallbackResources, NULL, 0);
  182.   GetResources(topLevel);
  183.  
  184.   mainPid = getpid();
  185.   setup_signal_handlers();
  186.   SetIcon(topLevel);
  187.  
  188.   printf("\r\n%s %s\r\n", "Seyon Copyright (c) 1992-1993 Muhammad M. Saggaf.",
  189.          "All rights reserved.");
  190.   printf("\rVersion %s rev. %s %s@%s %s-%s %s %s.\r\n\n", VERSION, REVISION, 
  191.          COMPILE_BY, COMPILE_HOST, COMPILE_HOSTTYPE, COMPILE_OSNAME, 
  192.          COMPILE_DATE, COMPILE_TIME);
  193.  
  194.   if ((tfp = fopen("/dev/tty", "r+")) == NULL) {
  195.     PopupInitError("errTtyAccess", exit);
  196.     goto MainLoop;
  197.   }
  198.  
  199.   tfd = fileno(tfp);
  200.  
  201.   io_get_attr(tfd, &oldmode);  /* get current console tty mode    */
  202.   newmode = oldmode;           /* copy (structure) to newmode  */
  203.  
  204. #if HAVE_TERMIOS || HAVE_TERMIO
  205.   newmode.c_oflag &= ~OPOST;
  206.   newmode.c_iflag |= (IGNBRK | IGNPAR);
  207.   newmode.c_iflag &= ~(IXON | IXOFF | ISTRIP | BRKINT);
  208.   newmode.c_lflag &= ~(ICANON | ISIG | ECHO);
  209.   newmode.c_cflag |= CREAD;
  210.   newmode.c_cc[VMIN] = 1;
  211.   newmode.c_cc[VTIME] = 1;
  212. #else
  213. #if HAVE_SGTTYB
  214.   newmode.sg_flags = CBREAK;
  215. #endif
  216. #endif
  217.  
  218.   set_tty_mode();
  219.  
  220.   InitVariables(topLevel);
  221.  
  222.   if (argc > 1 && strcmp(argv[1], "--")) {
  223.     SeErrorF("Unknown or incomplete command-line switch: `%s'", argv[1], 
  224.              "", "");
  225.     PopupInitError("errSwitches", do_exit);
  226.     goto MainLoop;
  227.   }
  228.  
  229.   /* Open modem port and configure it */
  230.  
  231.   strcpy(modemList, qres.modems);
  232.   curModem = GetFirstWord(modemList);
  233.  
  234.   show("Locating Modems...");
  235.  
  236.   do {
  237.     if ((retStatus = OpenModem(curModem)) >= 0) break;
  238.  
  239.     ShowOpenModemErrMsg(curModem, retStatus);
  240.     if (retStatus != ERR_MDM_NOMODEM)
  241.       show(FmtString("Modem ``%s'' is Unavailable.\n", curModem, "", ""));
  242.     curModem = GetNextWord();
  243.   } while (curModem[0] != '\0');
  244.  
  245.   if (retStatus < 0) {
  246.     SeError("No Modems Available");
  247.     PopupInitError("errModemInit", do_exit); 
  248.     goto MainLoop;
  249.   }
  250.  
  251.   show(FmtString("Modem ``%s'' is Available.\n", curModem, "", ""));
  252.  
  253.   pipe(mainToTermPipe);
  254.   StartTerminal();
  255.  
  256.   CreateCommandCenter();
  257.  
  258.   pipe(child_pipe);
  259.   XtAppAddInput(XtWidgetToApplicationContext(topLevel), child_pipe[0], 
  260.                 (XtPointer)XtInputReadMask, ExecProcRequest, NULL);
  261.   pipe(scriptToMainPipe);
  262.   XtAppAddInput(XtWidgetToApplicationContext(topLevel), scriptToMainPipe[0], 
  263.                 (XtPointer)XtInputReadMask, GetParameters, NULL);
  264.  
  265.   sprintf(startupAction, "%sMessage(\"Welcome to Seyon version %s%s\");", 
  266.           qres.startupAction, VERSION, REVISION);
  267.   if (qres.script) sprintf(startupAction + strlen(startupAction), 
  268.                            "RunScript(%s);", qres.script);
  269.   if (qres.dialAutoStart) strcat(startupAction, "DialEntries(Default);");
  270.  
  271.   linkflag = 2;
  272.   DispatchActions(ACTION_DISPATCH, startupAction, genericWidget);
  273.  
  274. MainLoop:
  275.   XtAppMainLoop(app_con);
  276.   return 0;
  277. }
  278.  
  279. void
  280. setup_signal_handlers()
  281. {
  282.   signal(SIGINT, SIG_IGN);
  283.   signal(SIGQUIT, SIG_IGN);
  284.  
  285. #ifdef SIGBUS
  286.   signal(SIGBUS, die);
  287. #endif
  288.   signal(SIGFPE, die);
  289.   signal(SIGILL, die);
  290.   signal(SIGIOT, die);
  291.   signal(SIGSEGV, die);
  292.   signal(SIGTERM, die);
  293.   signal(SIGTRAP, die);
  294. }
  295.  
  296. void
  297. die(sig)
  298.      int             sig;
  299. {
  300.   void               KillChildProc();
  301.   signal(sig, SIG_IGN);
  302.  
  303.   SeErrorF("Killed by signal %d", sig, "", "");
  304.   SeNoticeF("Debugging info: pid=%d.", getpid(), "", "");
  305.  
  306.   if (getpid() == mainPid) {
  307.     KillTerminal();
  308.     KillChildProc();
  309.     
  310.     SeNotice("Press any key to exit");
  311.     getchar();
  312.     cleanup_exit(1);
  313.   }
  314.   else {
  315.     write_child_info(child_pipe, KILL_TERM, "Terminal Proc Exited");
  316.     exit(1);
  317.   }
  318. }
  319.  
  320. void
  321. do_exit(rc)
  322.      int             rc;
  323. {
  324.   void               KillChildProc();
  325.  
  326.   XtUnmapWidget(topLevel);
  327.   KillTerminal();
  328.   KillChildProc();
  329.  
  330.   unlock_tty();
  331.  
  332.   fflush(tfp);
  333.   restore_orig_mode();
  334.  
  335.   fclose(tfp);
  336.   close_modem();
  337.  
  338.   XtDestroyApplicationContext(app_con);
  339.   exit(rc);
  340. }
  341.  
  342. void
  343. cleanup_exit(status)
  344.      int             status;
  345. {
  346.   SeNotice("cleaning up..");
  347.   do_exit(status);
  348. }
  349.  
  350. void
  351. s_exit()
  352. {
  353.   show("I'm rated PG-34!!");
  354.   do_exit(0);
  355. }
  356.